#!/bin/sh
LANG=en_US.UTF-8

# web service image install 
echo "docker 创建 ubuntu镜像内安装 ssh vim supervisord"


#创建容器初始化脚本
echo "
#!/bin/sh

LANG=en_US.UTF-8
echo "更新服务"
rm -fv /etc/apt/sources.list /etc/apt/sources.list.d/rocm.list

#更换阿里软件源 16
cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main

deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main

deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe

deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe


EOF

#更新软件
apt-get -y update && apt-get -y upgrade 

#安装中文语音包
apt-get -y install  language-pack-zh-han*
apt -y install $(check-language-support)

cat > /etc/default/locale <<EOF

LANG=\"zh_CN.UTF-8\"
LANGUAGE=\"zh_CN:zh\"
LC_NUMERIC=\"zh_CN\"
LC_TIME=\"zh_CN\"
LC_MONETARY=\"zh_CN\"
LC_PAPER=\"zh_CN\"
LC_NAME=\"zh_CN\"
LC_ADDRESS=\"zh_CN\"
LC_TELEPHONE=\"zh_CN\"
LC_MEASUREMENT=\"zh_CN\"
LC_IDENTIFICATION=\"zh_CN\"
LC_ALL=\"zh_CN.UTF-8\"

EOF



cat >> /etc/environment << EOF


LANG=\"zh_CN.UTF-8\"
LANGUAGE=\"zh_CN:zh\"
LC_NUMERIC=\"zh_CN\"
LC_TIME=\"zh_CN\"
LC_MONETARY=\"zh_CN\"
LC_PAPER=\"zh_CN\"
LC_NAME=\"zh_CN\"
LC_ADDRESS=\"zh_CN\"
LC_TELEPHONE=\"zh_CN\"
LC_MEASUREMENT=\"zh_CN\"
LC_IDENTIFICATION=\"zh_CN\"
LC_ALL=\"zh_CN.UTF-8\"

EOF



cat >> /etc/profile << EOF

LANG=\"zh_CN.UTF-8\"

EOF





apt-get -y update && apt-get -y upgrade 
#安装 supervisor 进程管理服务
apt-get install -y  epel-release
apt-get install -y  supervisor


#安装ssh服务
apt-get install -y  passwd
apt-get install -y  openssl
apt-get install -y  openssh-server
apt-get install -y  vim


#配置ssh服务
ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' -y
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' -y
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N '' -y

#设置 ssh 配置文件
sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation\\ no/g" /etc/ssh/sshd_config

sed -i "s/PermitRootLogin.*/PermitRootLogin\\ yes/g" /etc/ssh/sshd_config
sed -i "s/UsePAM.*/UsePAM\\ yes/g" /etc/ssh/sshd_config

#修改root密码
echo "root:1813033378" | chpasswd



#生成容器内服务启动 /etc/supervisor/conf.d
cat > /etc/supervisor/conf.d/ssh.conf << EOF
[supervisord]
nodaemon=true
; ssh 服务
[program:ssh]
command=/usr/sbin/sshd


EOF
mkdir -p /run/sshd

#删除初始脚本
rm -f /init-ubuntu-x86-x64.sh
#运行程序 
#supervisord -c /etc/supervisor/supervisord.conf

" >init-ubuntu-x86-x64.sh



#创建Dockerfile镜像构建文件
cat > Dockerfile << EOF
#导入镜像
FROM rocm/dev-ubuntu-16.04



#复制工程脚本到镜像去
COPY init-ubuntu-x86-x64.sh /
#添加脚本权限
RUN chmod 777 /init-ubuntu-x86-x64.sh && /init-ubuntu-x86-x64.sh

LABEL u="ubuntu镜像内安装 ssh vim supervisord root 1813033378"

#运行程序 supervisord -c /etc/supervisord.conf
 # 定参
ENTRYPOINT ["supervisord", "-c"]
 # 变参
CMD ["/etc/supervisor/supervisord.conf"] 


EOF


#docker 构建镜像 后台 脚本


cat > buildim.sh << EOF
#!/bin/sh
LANG=en_US.UTF-8
#构建镜像
docker build -f $(pwd)/Dockerfile -t  hq/ubuntu-16.04-ssh $(pwd)/
#启动容器
docker run --name ssh-ubunt16 -d -v /mnt/:/mnt -p 8888:8888 -p 222:22 -p 81:80 hq/ubuntu-16.04-ssh
#删除脚本
rm -vf Dockerfile install.sh init-ubuntu-x86-x64.sh buildim.sh ubuntu-16.04-x86-x64-ssh-dockerFile.sh
docker ps -a
docker images
sync
 
EOF

#后台构建镜像
chmod 777 buildim.sh
nohup $(pwd)/buildim.sh >> /tmp/docket-build-image.log 2>&1 &
#进度查看
echo "进度查看  /tmp/docket-build-image.log"
echo "tail -f /tmp/docket-build-image.log"
#tail -f /tmp/docket-build-image.log


